package com.atguigu.thread2;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionTest2 {
    public static void main(String[] args) {

        PrintTest printTest = new PrintTest();
        new Thread(()->{
            try {
                printTest.print1_52();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            },"A").start();
        new Thread(()->{
            try {
                printTest.printA_Z();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A" ).start();
    }
}
class PrintTest{
    private int num = 1;
    private final Lock lock = new ReentrantLock();
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    public void print1_52() throws InterruptedException {
        lock.lock();
        try {
            for (int i = 1; i < 53; i++) {
                while(num%3==0){
                    condition1.await();
                }
                System.out.print(i);
                num++;
                condition2.signal();
            }
        } finally {
            lock.unlock();
        }
    }
    public void printA_Z() throws InterruptedException {
        lock.lock();
        try {
            for (char i = 'A'; i <= 'Z'; i++) {
                while(num%3!=0){
                    condition2.await();
                }
                System.out.print(i);
                num++;
                condition1.signal();
            }
        } finally {
            lock.unlock();
        }
    }
}
